using System.Drawing;
using System.Xml;

namespace ICSharpCode.TextEditor.Document
{
    public sealed class Span
    {
        private bool stopEOL;

        private HighlightColor color;

        private HighlightColor beginColor;

        private HighlightColor endColor;

        private char[] begin;

        private char[] end;

        private string name;

        private string expression;

        private string rule;

        private HighlightRuleSet ruleSet;

        private char escapeCharacter;

        private bool ignoreCase;

        private bool isBeginSingleWord;

        private bool? isBeginStartOfLine;

        private bool isEndSingleWord;

        internal HighlightRuleSet RuleSet
        {
            get
            {
                return ruleSet;
            }
            set
            {
                ruleSet = value;
            }
        }

        public bool IgnoreCase
        {
            get
            {
                return ignoreCase;
            }
            set
            {
                ignoreCase = value;
            }
        }

        public bool StopEOL => stopEOL;

        public bool? IsBeginStartOfLine => isBeginStartOfLine;

        public bool IsBeginSingleWord => isBeginSingleWord;

        public bool IsEndSingleWord => isEndSingleWord;

        public HighlightColor Color => color;

        public HighlightColor BeginColor
        {
            get
            {
                if (beginColor != null)
                {
                    return beginColor;
                }
                return color;
            }
        }

        public HighlightColor EndColor => (endColor != null) ? endColor : color;

        public char[] Begin => begin;

        public char[] End => end;

        public string Name => name;

        public string Rule => rule;

        public string Expression => expression;

        public char EscapeCharacter => escapeCharacter;

        public Span(XmlElement span, int alpha, Color blackColor)
        {
            color = new HighlightColor(span, alpha, blackColor);
            if (span.HasAttribute("rule"))
            {
                rule = span.GetAttribute("rule");
            }
            if (span.HasAttribute("escapecharacter"))
            {
                escapeCharacter = span.GetAttribute("escapecharacter")[0];
            }
            name = span.GetAttribute("name");
            if (span.HasAttribute("stopateol"))
            {
                stopEOL = bool.Parse(span.GetAttribute("stopateol"));
            }
            if (span["Begin"] != null)
            {
                begin = span["Begin"].InnerText.ToCharArray();
                beginColor = new HighlightColor(span["Begin"], color, alpha, blackColor);
                if (span["Begin"].HasAttribute("singleword"))
                {
                    isBeginSingleWord = bool.Parse(span["Begin"].GetAttribute("singleword"));
                }
                if (span["Begin"].HasAttribute("startofline"))
                {
                    isBeginStartOfLine = bool.Parse(span["Begin"].GetAttribute("startofline"));
                }
            }
            if (span["End"] != null)
            {
                end = span["End"].InnerText.ToCharArray();
                endColor = new HighlightColor(span["End"], color, alpha, blackColor);
                if (span["End"].HasAttribute("singleword"))
                {
                    isEndSingleWord = bool.Parse(span["End"].GetAttribute("singleword"));
                }
            }
            if (span["Expression"] != null)
            {
                beginColor = new HighlightColor(span["Expression"], color, alpha, blackColor);
                endColor = new HighlightColor(span["Expression"], color, alpha, blackColor);
                expression = span["Expression"].InnerText.ToString();
            }
        }
    }
}
